package net.omnivention.proxy;

import java.util.HashMap;
import java.util.Set;

public class HttpHeaders {

	private HashMap<String, String> map;

	public HttpHeaders() {
		this.map = new HashMap<String, String>();
	}

	void add(String line) {
		int c = line.indexOf(":");
		if (c > -1) {
			String name = line.substring(0, c).toLowerCase();
			String value = line.substring(c + 1, line.length()).trim();
			map.put(name, value);
		}
	}

	public String getHeaderValue(String headerName) {
		return map.get(headerName.toLowerCase());
	}

	public void deleteHeader(String headerName) {
		map.remove(headerName);
	}

	public byte[] getAsByteArray() {
		return toString().getBytes();
	}

	public String toString() {
		StringBuffer s = new StringBuffer();

		Set<String> headerNames = map.keySet();
		for (String name : headerNames) {
			String value = map.get(name);
			s.append(name + ": " + value + "\r\n");
		}

		return s.toString();
	}

}
